/*
 * PesquisarTurma.java
 *
 * Created on 13 de Junho de 2010, 18:46
 */

package virtual.view;

import java.util.List;
import javax.swing.table.DefaultTableModel;
import virtual.BD.Turma;
import virtual.service.TurmaJpaController;

/**
 *
 * @author  onyas
 */
public class PesquisarTurma extends javax.swing.JPanel {

    private TurmaJpaController service;
    
    /** Creates new form PesquisarTurma */
    public PesquisarTurma() {
        initComponents();
        
        service = new TurmaJpaController();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jlNome = new javax.swing.JLabel();
        jtfNomeTurma = new javax.swing.JTextField();
        jbPesquisar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtTurma = new javax.swing.JTable();
        jbSelecionar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();

        jlNome.setText("Nome: ");
        jlNome.setName("jlNome"); // NOI18N

        jtfNomeTurma.setName("jtfNomeTurma"); // NOI18N

        jbPesquisar.setText("Pesquisar");
        jbPesquisar.setName("jbPesquisar"); // NOI18N
        jbPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbPesquisarActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtTurma.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jtTurma.setToolTipText("Resultado da pesquisa");
        jtTurma.setName("jtTurma"); // NOI18N
        jScrollPane1.setViewportView(jtTurma);

        jbSelecionar.setText("Selecionar");
        jbSelecionar.setName("jbSelecionar"); // NOI18N
        jbSelecionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSelecionarActionPerformed(evt);
            }
        });

        jbCancelar.setText("Cancelar");
        jbCancelar.setName("jbCancelar"); // NOI18N
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jlNome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfNomeTurma, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jbPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbSelecionar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlNome)
                    .addComponent(jbPesquisar)
                    .addComponent(jtfNomeTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbSelecionar)
                    .addComponent(jbCancelar))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

/**
 * Pesquisa as turmas cadastradas no bando de dados.
 * @param evt evento de ação.
 */
private void jbPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbPesquisarActionPerformed
    
    jtTurma.setVisible(true);
    
    String param = this.jtfNomeTurma.getText();
    List<Turma> listaTurma = (List)service.findTurmaEntities(param);
    
    System.out.println("Teste da lista: "+listaTurma.get(0).getNmTurma());
    
    
    DefaultTableModel modelo = new DefaultTableModel();
    modelo.addColumn("Código");
    modelo.addColumn("Nome");
    for (int i=0; i<listaTurma.size(); i++) {
        String[] dados = new String[2];
        dados[0] = listaTurma.get(i).getCdTurma().toString();
        dados[1] = listaTurma.get(i).getNmTurma();
        modelo.addRow(dados);
    } // Fim do for
    
    
    jtTurma.setModel(modelo);
    
    
    jtTurma.repaint();
    
    
}//GEN-LAST:event_jbPesquisarActionPerformed


/**
 * Seleciona uma linha da tabela e abre na tela de edição.
 * @param evt evento de ação.
 */
private void jbSelecionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSelecionarActionPerformed
    
    String codigo = jtTurma.getModel().getValueAt(jtTurma.getSelectedRow(), 0).toString();
    Turma turma = service.findTurma(Integer.parseInt(codigo));
    
    CadastroTurma painel = new CadastroTurma(turma);
    
    Principal.abreNovaJanelaInterna(painel);
    
}//GEN-LAST:event_jbSelecionarActionPerformed

/**
 * Cancela a ação e volta para a tela de cadastro.
 * @param evt evento de ação.
 */
private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
    
    CadastroTurma jp = new CadastroTurma();
    
    Principal.abreNovaJanelaInterna(jp);
    
}//GEN-LAST:event_jbCancelarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbPesquisar;
    private javax.swing.JButton jbSelecionar;
    private javax.swing.JLabel jlNome;
    private javax.swing.JTable jtTurma;
    private javax.swing.JTextField jtfNomeTurma;
    // End of variables declaration//GEN-END:variables

}
